#!/usr/bin/env python3
"""
Simplified FastAPI server for testing basic functionality
"""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

# Create FastAPI app
app = FastAPI(
    title="UmaInsight Backend API",
    description="UMA/UKM Analytics Platform Backend",
    version="2.0.0"
)

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:3000",
        "http://127.0.0.1:3000",
        "http://localhost:3001"
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Basic routes
@app.get("/")
async def root():
    return {"message": "UmaInsight Backend API v2.0.0"}

@app.get("/health")
async def health_check():
    return {"status": "ok", "version": "2.0.0"}

# Mock auth endpoints
@app.post("/api/v1/auth/login")
async def login():
    return {
        "success": True,
        "user": {
            "user_id": "dev-user",
            "email": "dev@example.com",
            "name": "Development User"
        },
        "token": "dev-token",
        "message": "Authentication successful"
    }

@app.get("/api/v1/auth/me")
async def get_current_user():
    return {
        "success": True,
        "user": {
            "user_id": "dev-user",
            "email": "dev@example.com",
            "name": "Development User"
        },
        "message": "User information retrieved successfully"
    }

# Mock telemetry endpoints
@app.get("/api/v1/telemetry/")
async def get_telemetry_points():
    return {
        "success": True,
        "data": [
            {
                "id": 1,
                "name": "WebCore.Memory.Allocated",
                "type": "histogram",
                "description": "Memory allocated by WebCore",
                "units": "bytes",
                "component": "WebCore",
                "file_path": "components/webcore/memory_metrics.h",
                "line_number": 123,
                "owner": "webcore-team@example.com",
                "assigned_team_id": "team-1",
                "assigned_team_name": "WebCore Team"
            }
        ],
        "total": 1,
        "page": 1,
        "page_size": 50
    }

@app.get("/api/v1/telemetry/stats/overview")
async def get_telemetry_stats():
    return {
        "success": True,
        "data": {
            "total_metrics": 1000,
            "by_type": {
                "histogram": 800,
                "ukm_event": 150,
                "ukm_metric": 50
            },
            "top_components": {
                "WebCore": 200,
                "Blink": 150,
                "V8": 100
            },
            "unassigned": 50
        },
        "message": "Telemetry statistics retrieved successfully"
    }

# Mock teams endpoints
@app.get("/api/v1/teams/")
async def get_teams():
    return [
        {
            "id": "team-1",
            "name": "WebCore Team",
            "description": "WebCore component team",
            "created_by": "admin",
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": "2024-01-01T00:00:00Z",
            "member_count": 5,
            "assignment_count": 10
        }
    ]

# Mock search endpoints
@app.post("/api/v1/search/")
async def search_telemetry(request: dict):
    return {
        "success": True,
        "data": [
            {
                "id": 1,
                "name": "WebCore.Memory.Allocated",
                "type": "histogram",
                "description": "Memory allocated by WebCore",
                "units": "bytes",
                "component": "WebCore",
                "file_path": "components/webcore/memory_metrics.h",
                "line_number": 123,
                "owner": "webcore-team@example.com",
                "assigned_team_id": "team-1",
                "assigned_team_name": "WebCore Team",
                "match_score": 1.0,
                "match_fields": ["exact_match"]
            }
        ],
        "total": 1,
        "query": request.get("query", ""),
        "page": 1,
        "page_size": 50
    }

if __name__ == "__main__":
    import uvicorn
    print("🚀 Starting UmaInsight Backend API Server...")
    print("📍 Server will be available at: http://localhost:8000")
    print("📖 API Documentation: http://localhost:8000/docs")
    uvicorn.run(app, host="0.0.0.0", port=8000)